package cn.dxh.stagefinal.controller;

import cn.dxh.stagefinal.bean.Buy;
import cn.dxh.stagefinal.service.BuyService;
import cn.dxh.stagefinal.utils.BaseWriteInfo;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 你可别被我给逮到了
 * 2019/12/9/周一/19:37
 * 我们遇到什么困难都不要怕!
 * 微笑面对它!
 * 消除恐惧的最好办法就是面对恐惧，坚持就是胜利!
 * 加油!奥利给!
 */
@Controller
public class BuylistController
{
    @Autowired
    private BuyService buyService;

    /*//查询所有  分页
    @RequestMapping("/buy/getAllList")
    public String getAllList(ModelMap map)
    {
        List<Buy> buyList = buyService.findBuyList();
        map.addAttribute("buyList",buyList);
        return "buy/PurchasingList";
    }*/


    //条件查询  分页 时间、供应商、状态模糊查询
    @RequestMapping("/buy/getList")
    public String getAllList (@RequestParam(defaultValue = "1") Integer pageIndex,
                              @RequestParam(defaultValue = "") String beginTime,
                              @RequestParam(defaultValue = "") String endTime,
                              @RequestParam(defaultValue = "0") Integer state,
                              String supplier,
                              ModelMap map
                              )
    {
        if (pageIndex == 0)
        {
            pageIndex = 1;
        }

        PageInfo<Buy> pageInfo = buyService.findLikeList(pageIndex,beginTime, endTime, supplier, state);
        map.addAttribute("pageList",pageInfo);
        //存储回显的模糊查询
        map.addAttribute("beg",beginTime);
        map.addAttribute("end",endTime);
        map.addAttribute("sup",supplier);
        map.addAttribute("sta",state);
        return "buy/PurchasingList";
    }

    // CustomDateEditor为自定义日期编辑器
   /* @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request)
    {
        //转换日期
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
    }
*/

    //跳转到添加页面
    @RequestMapping("/buy/add")
    public String add()
    {

        return "buy/addSingle";
    }

    //添加商品  仅支持单件添加
    @RequestMapping("/buy/addSingle")
    public void addSingle(String id,Integer supCode,String supName,String payType,String person,String creTime,String desc,String brand,Integer code,String name,Integer price,Integer count,Integer amount,HttpServletResponse response) throws IOException
    {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = response.getWriter();
        int i = buyService.insert(id, supCode, supName, payType, person, creTime, desc, brand, code, name, price, count, amount);
        if (i>0)
        {
            writer.println(String.format(BaseWriteInfo.ALERT_AND_REDIRECT, "新增成功", "/buy/getList"));
        }
        else
        {
            writer.println(String.format(BaseWriteInfo.ALERT_AND_BACK, "新增失败"));
        }
    }



    //跳转编辑页面  并查询当前id的信息
    @RequestMapping("/buy/edit")
    public String edit(String nowId,ModelMap map)
    {
        Buy buy = buyService.findById(nowId);
        System.out.println(buy);
        map.addAttribute("nowBuy",buy);
        return "buy/edit";
    }

    //编辑修改
    @RequestMapping("/buy/update")
    public void update(String id, Integer supCode, String supName, String payType, String person, String creTime, String desc, String brand, Integer code, String name, Integer price, Integer count, Integer amount, HttpServletResponse httpServletResponse) throws IOException
    {
        httpServletResponse.setContentType("text/html;charset=UTF-8");
        PrintWriter out = httpServletResponse.getWriter();
        int i = buyService.update(id,supCode,supName,payType,person,creTime,desc,brand,code,name,price,count,amount);
        if (i>0)
        {
            //修改成功
            out.println(String.format(BaseWriteInfo.ALERT_AND_REDIRECT, "修改成功", "/buy/getList"));
        }
        else
        {
            //修改失败
            out.println(String.format(BaseWriteInfo.ALERT_AND_BACK, "修改失败"));
        }
    }



    //跳转审批页面  并查询当前id的信息
    @RequestMapping("/buy/toAudit")
    public String audit(String nowId,ModelMap map)
    {
        Buy buy = buyService.findById(nowId);
        System.out.println(buy);
        map.addAttribute("auditBuy",buy);
        return "buy/audit";
    }


    //审计审批
    @RequestMapping("/buy/audit")
    public void audit(String nowId,HttpServletResponse httpServletResponse) throws IOException
    {
        Integer state=2;   //待入库
        System.out.println("controller中的nowId="+nowId);
        httpServletResponse.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = httpServletResponse.getWriter();
        int i = buyService.updateState(nowId,state);
        System.out.println("controller执行后i="+i);
        if (i>0)
        {
            writer.println(String.format(BaseWriteInfo.ALERT_AND_REDIRECT, "审核通过", "/buy/getList"));
        }
        else
        {
            writer.println(String.format(BaseWriteInfo.ALERT_AND_BACK, "审核失败"));
        }
    }

    //查看 并跳转 查询
    @RequestMapping("/buy/check")
    public String check(String nowId,ModelMap map,Integer nowState)
    {
        Buy buy = buyService.findById(nowId);
        System.out.println(buy);
        map.addAttribute("checkBuy",buy);

        if (nowState!=4)   //仅支持查看   已退回
        {

            return "buy/check";
        }
        else  //已删除   查看后有删除功能
        {
            return "buy/check";
        }
    }



    //退单 并跳转
    @RequestMapping("/buy/backOrder")
    public String backOrder (String nowId, ModelMap map)
    {
        Buy buy = buyService.findById(nowId);
        System.out.println(buy);
        map.addAttribute("backOrder",buy);
        return "buy/backOrder";
    }

    //修改状态
    @RequestMapping("/buy/goBack")
    public void goBack(String nowId,HttpServletResponse httpServletResponse) throws IOException
    {
        Integer state=4;  // 退回中
        System.out.println("controller中的nowId="+nowId);
        httpServletResponse.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = httpServletResponse.getWriter();
        int i = buyService.updateState(nowId,state);
        System.out.println("controller执行后i="+i);
        if (i>0)
        {
            writer.println(String.format(BaseWriteInfo.ALERT_AND_REDIRECT, "退单成功", "/buy/getList"));
        }
        else
        {
            writer.println(String.format(BaseWriteInfo.ALERT_AND_BACK, "退单失败"));
        }
    }


    //删除订单  修改状态
    @RequestMapping("/buy/delete")
    public void delete(String deleteId,HttpServletResponse httpServletResponse) throws IOException
    {
        Integer state=6;   // 已删除
        System.out.println("controller中的nowId="+deleteId);
        httpServletResponse.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = httpServletResponse.getWriter();
        int i = buyService.updateState(deleteId,state);
        System.out.println("controller执行后i="+i);
        if (i>0)
        {
            writer.println(String.format(BaseWriteInfo.ALERT_AND_REDIRECT, "删除成功", "/buy/getBackList"));
        }
        else
        {
            writer.println(String.format(BaseWriteInfo.ALERT_AND_BACK, "删除失败"));
        }
    }



    @RequestMapping("/buy/getBackList")
    public String getBackList (@RequestParam(defaultValue = "1") Integer pageIndex,
                              @RequestParam(defaultValue = "") String beginTime,
                              @RequestParam(defaultValue = "") String endTime,
                              @RequestParam(defaultValue = "0") Integer state,
                              String supplier,
                              ModelMap map)
    {
        if (pageIndex == 0)
        {
            pageIndex = 1;
        }

        PageInfo<Buy> pageInfo = buyService.findBackList(pageIndex,beginTime, endTime, supplier, state);
        map.addAttribute("backPageList",pageInfo);
        //存储回显的模糊查询
        map.addAttribute("beg",beginTime);
        map.addAttribute("end",endTime);
        map.addAttribute("sup",supplier);
        map.addAttribute("sta",state);
        return "buy/backList";
    }


    //查看 并跳转 查询 退单
    @RequestMapping("/buy/checkBack")
    public String checkBack(String nowId,ModelMap map,Integer nowState)
    {
        Buy buy = buyService.findById(nowId);
        System.out.println(buy);
        map.addAttribute("checkBackBuy",buy);
        if (nowState==4)
        {
            return "buy/checkBackFtl";

        }
        else  //可以撤销//已完成退单
        {
            return "buy/delete"; //已退单完成的可以删除
        }
    }


    /*//删除订单
    @RequestMapping("/buy/deleteOrder")
    public void deleteOrder(String deleteId,HttpServletResponse response) throws IOException
    {
        PrintWriter writer = response.getWriter();
        response.setContentType("text/html;charset=UTF-8");
        int i = buyService.deleteOrder(deleteId);
        if (i>0)
        {
            //删除成功
            writer.println();
        }
    }*/

    //跳转到添加退单页面
    @RequestMapping("/buy/toAddBack")
    public String toAddBack()
    {

        return "buy/addBackOrder";
    }

    //添加退单
    @RequestMapping("/buy/addBackOrder")
    public void addBackOrder(String id,Integer supCode,String supName,String payType,String person,String creTime,String desc,String brand,Integer code,String name,Integer price,Integer count,Integer amount,HttpServletResponse response) throws IOException
    {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = response.getWriter();
        int i = buyService.addBackOrder(id, supCode, supName, payType, person, creTime, desc, brand, code, name, price, count, amount);
        if (i>0)
        {
            writer.println(String.format(BaseWriteInfo.ALERT_AND_REDIRECT, "新增成功", "/buy/getList"));
        }
        else
        {
            writer.println(String.format(BaseWriteInfo.ALERT_AND_BACK, "新增失败"));
        }
    }

    //撤销退单
    @RequestMapping("/buy/revocation")
    public void revocation(String id,HttpServletResponse response) throws IOException
    {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = response.getWriter();
        Integer state = 2;
        int i = buyService.updateState(id, state);
        if (i>0)
        {
            writer.println(String.format(BaseWriteInfo.ALERT_AND_REDIRECT, "撤销成功", "/buy/getBackList"));
        }
        else
        {
            writer.println(String.format(BaseWriteInfo.ALERT_AND_BACK, "撤销失败"));
        }
    }

    //完成退单
    @RequestMapping("/buy/complete")
    public void complete(String id,HttpServletResponse response) throws IOException
    {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter writer = response.getWriter();
        Integer state = 5;
        int i = buyService.updateState(id, state);

        if (i>0)
        {
            writer.println(String.format(BaseWriteInfo.ALERT_AND_REDIRECT, "退单完成", "/buy/getBackList"));
        }
        else
        {
            writer.println(String.format(BaseWriteInfo.ALERT_AND_BACK, "撤退单失败"));
        }
    }

}

